﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

using WCFMINERP.Contracts.DataContracts;

namespace WCFMINERP.Update
{
    public  class XMLOperator
    {
        XmlNode xmlNode;
        XmlElement xmlelem;
        /// <summary>
        /// 获取商户版本信息
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static ShopParametersInfo GetShopParameters(string xmlPath)
        {
            CreateShopParametersXml(xmlPath);
            ShopParametersInfo shopparas = new ShopParametersInfo();
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlPath);
            shopparas.ClientVersion = xml.SelectSingleNode(@"ShopParameters/ClientVersion").InnerText;
            shopparas.ProductVersion = xml.SelectSingleNode(@"ShopParameters/ProductVersion").InnerText;
            return shopparas;
        }

        /// <summary>
        /// 创建商户版本信息xml
        /// </summary>
        /// <param name="xmlpath"></param>
        public static void CreateShopParametersXml(string xmlpath)
        {
            if (!File.Exists(xmlpath))
            {
                XmlDocument xmldoc = new XmlDocument();
                
                //定义XML文档头文件
                XmlDeclaration xmlDeclaration = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmldoc.AppendChild(xmlDeclaration);
                //定义并添加XML的根
                XmlElement xmlroot = xmldoc.CreateElement("ShopParameters");
                xmldoc.AppendChild(xmlroot);
                XmlElement ClientVersion = xmldoc.CreateElement("ClientVersion");
                XmlText txClientVersion = xmldoc.CreateTextNode("1.0");
                ClientVersion.AppendChild(txClientVersion);
                XmlElement ProductVersion = xmldoc.CreateElement("ProductVersion");
                XmlText txProductVersion = xmldoc.CreateTextNode("0.0");
                ProductVersion.AppendChild(txProductVersion);
                xmlroot.AppendChild(ClientVersion);
                xmlroot.AppendChild(ProductVersion);
                xmldoc.Save(xmlpath);
                
            }
        }

        /// <summary>
        /// 创建产品信息xml文件
        /// </summary>
        /// <param name="xmlpath"></param>
        public static void CreateShopProductXml(List<ShopProductInfo> shopProductList, List<PaperFaceInfo> paperFaceInfoList, List<PaperTypeInfo> PaperTypeInfoList, List<CommonChargeItemInfo> commonChargeItemInfoList)
        {
            string xmlpath =Application.StartupPath+"\\product.xml";
            if (File.Exists(xmlpath))
            {
                File.Delete(xmlpath);
            }
            XmlDocument xmldoc = new XmlDocument();
                
            //定义XML文档头文件
            XmlDeclaration xmlDeclaration = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmldoc.AppendChild(xmlDeclaration);

            XmlProcessingInstruction xmlprocess = xmldoc.CreateProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"WCFERPSchemaProduct.xsl\"");
            xmldoc.AppendChild(xmlprocess);
            //定义并添加XML的根
            XmlElement xmlroot = xmldoc.CreateElement("orderProductInfo");
            xmldoc.AppendChild(xmlroot);

            //产品
            for (int i = 0; i < shopProductList.Count; i++)
            {
                XmlElement product = xmldoc.CreateElement("productInfo");
                product.SetAttribute("productid", shopProductList[i].productid.ToString());
                product.SetAttribute("productName", shopProductList[i].productName);
                product.SetAttribute("width", shopProductList[i].width.ToString());
                product.SetAttribute("height", shopProductList[i].height.ToString());
                product.SetAttribute("pageNum",shopProductList[i].pageNum.ToString());
                product.SetAttribute("status", shopProductList[i].status.ToString());
                product.SetAttribute("areaPrice",shopProductList[i].areaPrice.ToString());
                product.SetAttribute("unitPrice",shopProductList[i].unitPrice.ToString());
                product.SetAttribute("appendPrice",shopProductList[i].appendPrice.ToString());
                product.SetAttribute("mnemonicCode",shopProductList[i].mnemonicCode);
                product.SetAttribute("coverWidth",shopProductList[i].coverWidth.ToString());
                product.SetAttribute("coverHeight", shopProductList[i].coverHeight.ToString());
                product.SetAttribute("costTypeName",shopProductList[i].costTypeName.ToString());
                product.SetAttribute("productCategoryName",shopProductList[i].productCategoryName);
                product.SetAttribute("usedTimes", shopProductList[i].UsedTimes.ToString());
                if (string.IsNullOrEmpty(shopProductList[i].CommonItemsString))
                {
                    product.SetAttribute("CommonChargeItemIdList", "");
                }
                else
                {
                    product.SetAttribute("CommonChargeItemIdList", shopProductList[i].CommonItemsString.ToString());
                }
                xmlroot.AppendChild(product);
            }
            //纸面
            for (int i = 0; i < paperFaceInfoList.Count; i++)
            {
                XmlElement paperFace = xmldoc.CreateElement("paperFace");
                paperFace.SetAttribute("paperFaceId", paperFaceInfoList[i].PaperFaceId.ToString());
                paperFace.SetAttribute("paperFaceName",paperFaceInfoList[i].PaperFaceName);
                paperFace.SetAttribute("paperFaceName", paperFaceInfoList[i].PaperFaceName);
                paperFace.SetAttribute("defaultPaperFace", paperFaceInfoList[i].DefaultPaperFace.ToString());
                xmlroot.AppendChild(paperFace);
            }

            //纸型
            for (int i = 0; i < PaperTypeInfoList.Count; i++)
            {
                XmlElement paperType = xmldoc.CreateElement("paperType");
                paperType.SetAttribute("paperTypeId",PaperTypeInfoList[i].paperTypeId.ToString());
                paperType.SetAttribute("paperTypeName",PaperTypeInfoList[i].paperTypeName);
                paperType.SetAttribute("areaPrice",PaperTypeInfoList[i].areaPrice.ToString());
                paperType.SetAttribute("defaultPaperType", PaperTypeInfoList[i].DefaultPaperType.ToString());
                xmlroot.AppendChild(paperType);
            }
            
            //公共计价项目
            foreach (var item in commonChargeItemInfoList)
            {
                XmlElement commonChargeItem = xmldoc.CreateElement("commonChargeItem");
                commonChargeItem.SetAttribute("CommonChargeItemId", item.CommonChargeItemId.ToString());
                commonChargeItem.SetAttribute("CommonChargeItemName", item.CommonChargeItemName);
                xmlroot.AppendChild(commonChargeItem);
            }
    
            xmldoc.Save(xmlpath);
        }

        public static void UpdateNodesValue(string nodesPath,string xmlPath,string nodesValue)
        {
            
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlPath);
        
            xmldoc.SelectSingleNode(nodesPath).InnerText = nodesValue;
            xmldoc.Save(xmlPath);
          
        
        }
    }
}
